CREATE Procedure usp_Get_Order_No
	@System_Name nvarchar(20)	--系统模块名称
	@Order_NO nvarchar(22) OUTPUT
AS
BEGIN	
	declare @OI_ID		 int
	declare @System_Name nvarchar(20)
	declare @Order_Pre   nvarchar(10)

	declare @order_number int

	declare @OS_Date    nvarchar(10)
	declare @OS_ID		int


	set @OS_Date = replace(convert(varchar(10),getdate(),120),'-','')

	declare @Order_No    nvarchar(22)

	set @System_Name='酒店'

	set @OS_ID =0
	set @Order_No=''

	select @OI_ID=OI_ID,@Order_Pre = ISNULL(Order_Pre,'') FROM Order_Info WHERE System_Name = @System_Name

	--SELECT 
	if @OI_ID IS NOT NULL 
	begin
		if not exists(SELECT OS_ID FROM Order_Serial WHERE OI_ID = @OI_ID AND OS_Date=@OS_Date)
			insert into Order_Serial(OI_ID,OS_Date,OS_Serial_Number) values(@OI_ID,@OS_Date,0)
		
		
		
		select @OS_ID = OS_ID,@order_number = OS_Serial_Number + 1 FROM Order_Serial WHERE OI_ID = @OI_ID
	 

		--select  replace(convert(varchar(10),getdate(),120),'-','')

		SELECT   @Order_No = 
			  CASE len(convert(varchar(10),@order_number))
				 WHEN 1 THEN  @Order_Pre + @OS_Date + '00000' + convert(varchar(10),@order_number)
				 WHEN 2 THEN  @Order_Pre + @OS_Date + '0000' + convert(varchar(10),@order_number)
				 WHEN 3 THEN  @Order_Pre + @OS_Date + '000' + convert(varchar(10),@order_number)
				 WHEN 4 THEN  @Order_Pre + @OS_Date + '00' + convert(varchar(10),@order_number)
				 WHEN 5 THEN  @Order_Pre + @OS_Date + '0' + convert(varchar(10),@order_number)	         
				 WHEN 6 THEN  @Order_Pre + @OS_Date + convert(varchar(10),@order_number)	         
				 ELSE  ''
			  END
	end 
	else 
	set @Order_No = '' --系统没有设置订单系统表时输出订单号没空


	select @Order_No,@OS_ID 

	
	SELECT @Order_NO = @tmpOrder_NO
  
END

GO
